//
//  CoinConvertController.swift
//  SCA
//
//  Created by tjun on 2025/9/13.
//

import UIKit

class CoinConvertController: BaseController {
    
    private let titleLabel = UILabel(text: "可领取奖励".localizedString, font: UIFontMediumMake(14), textColor: .white)
    private let valueLabel = UILabel(text: "$0.0000", font: UIFontSemiboldMake(30), textColor: .white)
    private let confirmButton = MainButton(type: .system, title: "立即转换".localizedString, font: UIFontMake(16), textColor: .white)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        titleLabel.addTo(self.view)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(navBarHeight + 30)
            make.centerX.equalToSuperview()
        }
        
        valueLabel.addTo(self.view)
        valueLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
        }
        
        let awardView = UIView().addTo(self.view)
        awardView.isHidden = true
        awardView.backgroundColor = UIColorMake(0x2E323F)
        awardView.addCorner(radius: 10)
        awardView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(valueLabel.snp.bottom).offset(30)
            make.height.equalTo(118)
        }
        
        let totalAwardView = UIView().addTo(awardView)
        totalAwardView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(54)
        }
                
        let mineAwardLabel = UILabel(text: "我的奖励".localizedString, font: UIFontSemiboldMake(16), textColor: .white).addTo(totalAwardView)
        mineAwardLabel.snp.makeConstraints { make in
            make.left.equalTo(AppStyle.margin.left)
            make.centerY.equalToSuperview()
        }
        
        let totalAwardValueLabel = UILabel(text: "$0.0000", font: UIFontSemiboldMake(16), textColor: .white).addTo(totalAwardView)
        totalAwardValueLabel.snp.makeConstraints { make in
            make.right.equalTo(-AppStyle.margin.left)
            make.centerY.equalToSuperview()
        }

        let lineView = UIView().addTo(awardView)
        lineView.backgroundColor = .white.withAlphaComponent(0.3)
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.center.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let awardInfoView = UIView().addTo(awardView)
        awardInfoView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(totalAwardView.snp.bottom)
            make.height.equalTo(totalAwardView)
        }
        
        let iconImageView = UIImageView().addTo(awardInfoView)
        iconImageView.snp.makeConstraints { make in
            make.left.equalTo(mineAwardLabel)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(30)
        }
        
        let coinLabel = UILabel(font: UIFontSemiboldMake(16), textColor: .white).addTo(awardInfoView)
        coinLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(8)
            make.centerY.equalTo(iconImageView)
        }
        
        let coinAwardValueLabel = UILabel(text: "$0.00", font: UIFontSemiboldMake(16), textColor: .white).addTo(awardInfoView)
        coinAwardValueLabel.snp.makeConstraints { make in
            make.right.equalTo(totalAwardValueLabel)
            make.centerY.equalToSuperview()
        }
        
        let hintLabel = UILabel(text: "奖励将被转换成 USCA".localizedString, font: UIFontMake(12), textColor:UIColorMake(0x959595), textAlignment: .left).addTo(self.view)
        hintLabel.isHidden = true
        hintLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(awardView.snp.bottom).offset(20)
        }

        confirmButton.addTo(view)
        confirmButton.isEnabled = false
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        confirmButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.height.equalTo(AppStyle.size.buttonHeight.scaled)
            make.bottom.equalTo(-tabBarSafeBottomMargin - 40)
        }
        
        ComputingPowerService.loadConvertData(success:{ model in
            
            hintLabel.isHidden = false
            awardView.isHidden = false
        
            coinLabel.text = model.coinName
            hintLabel.text = String(format: "奖励将被转换成 %@".localizedString, model.coinName)
            iconImageView.setImage(url: model.icon)
            totalAwardValueLabel.text = model.value_disp
            coinAwardValueLabel.text = model.amount_disp
            
            self.valueLabel.text = model.value_disp
            self.confirmButton.isEnabled = model.claimable
        })
    }
    
    @objc func confirmButtonTapped() {
        HUD.showLoadingInView(self.view)
        ComputingPowerService.convert {
            HUD.showMessage("转换成功".localizedString)
        } failure: { error in
            HUD.showErrorMessage(error)
        }
    }
    
    
}

